package com.peteralbus.controller;
import com.peteralbus.entity.EarthquakeInfo;
import com.peteralbus.service.EarthquakeInfoService;
import com.peteralbus.util.GeoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
 * The type Earthquake info controller.
 *
 * @author PeterAlbus
 */
@Slf4j
@Api(value = "api of earthquake info") //是用在类上，表明是swagger资源
@CrossOrigin //@CrossOrigin注解的功能:禁止Ajax访问当前站点外的资源如,禁止在另一个站点使用ajax访问你的站点
@RestController//=@Controller + @ResponseBody 在一个类上添加@Controller注解，表明了这个类是一个控制器类
// 如果要求方法返回的是json格式数据，而不是跳转页面，可以直接在类上标注@RestController
@RequestMapping("/earthquakeInfo")
//作用：将请求和处理请求的控制器方法关联起来，建立映射关系。
//位置：1、标识类：设置映射请求的请求路径的初始信息
//2、表示方法：设置映射请求的请求路径的具体信息
//默认只写一个参数的话，就是给value赋值
public class EarthquakeInfoController
{
    /**
     * The Earthquake info service.
     */
    EarthquakeInfoService earthquakeInfoService;

    /**
     * Sets earthquake info service.
     *
     * @param earthquakeInfoService the earthquake info service
     */
    @Autowired  //可不可以是EarthquakeInfoServiceImpl
    public void setEarthquakeInfoService(EarthquakeInfoService earthquakeInfoService)
    {
        this.earthquakeInfoService = earthquakeInfoService;
    }

    /**
     * Gets all earthquake.
     *
     * @return the all earthquake
     */
    @ApiOperation(value = "get all earthquake")
    @GetMapping("/getAllEarthquake")
    List<EarthquakeInfo> getAllEarthquake( @RequestParam(defaultValue = "5" ) Double magnitude)
    {
        Map<String,Object> hashMap=new HashMap<>();
        hashMap.put("magnitude",magnitude);
        List<EarthquakeInfo> list=earthquakeInfoService.queryInfoWithLine(hashMap);
        EarthquakeInfo earthquakeInfo1 = null;
        List<EarthquakeInfo> list1=new ArrayList<>();
        for (EarthquakeInfo earthquakeInfo : list) {
            if(Objects.equals(earthquakeInfo.getEarthquakeName(), "云南大理州漾濞县"))
                list1.add(earthquakeInfo);
        }
        for (EarthquakeInfo earthquakeInfo : list1) {
            if(earthquakeInfo.getEarthquakeTime().toString().equals("2021-05-21T21:48:34"))
            {
                earthquakeInfo1=earthquakeInfo;
            }
            list.remove(earthquakeInfo);
            list.add(0,earthquakeInfo);
        }
        list.remove(earthquakeInfo1);
        list.add(0,earthquakeInfo1);
//        log.info("return");
        return list;
    }
    @GetMapping("/getAllEarthquakePosition")
    List<List<Double>> getAllEarthquakePosition(@RequestParam(defaultValue = "4" ) Double magnitude){
        Map<String,Object> hashMap=new HashMap<>();
        hashMap.put("magnitude",magnitude);
        List<List<Double>> positionList=new ArrayList<>();
        List<EarthquakeInfo> list=earthquakeInfoService.queryInfoWithLine(hashMap);
        for (EarthquakeInfo earthquakeInfo : list) {
            List<Double> list1=new ArrayList<>();
            list1.add(0,earthquakeInfo.getLongitude());
            list1.add(1,earthquakeInfo.getLatitude());
            positionList.add(list1);
        }
        return positionList;
    }
    @GetMapping("/getSeismicZonesData")
    List<EarthquakeInfo> getSeismicZonesData(Double left,Double right,Double top,Double bottom){
        return earthquakeInfoService.getSeismicZonesData(left,right,top,bottom);
    }
    /**
     * Gets earthquake by id.
     *
     * @param earthquakeId the earthquake info id
     * @return the earthquake by id
     */
    @GetMapping("/getEarthquakeById")
    EarthquakeInfo getEarthquakeById(Long earthquakeId)
    {
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("earthquakeId",earthquakeId);
        return earthquakeInfoService.queryInfoWithLine(mapParameter).get(0);
    }
    /**
     * Gets earthquake by condition.
     *
     * @param area   the area
     * @param start the start time(string)
     * @param end   the end time(string)
     * @return the earthquake by condition
     */
    @GetMapping("/getEarthquakeByCondition")
    List<EarthquakeInfo> getEarthquakeByCondition(String area, String start, String end,double low,double high)
    {
//        log.info(low+" "+high);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime=LocalDateTime.parse(start,df);
        LocalDateTime endTime=LocalDateTime.parse(end,df);
        List<EarthquakeInfo> result=new ArrayList<>();
        List<EarthquakeInfo> earthquakeInfoList=earthquakeInfoService.queryInfoWithLine(null);
        for(EarthquakeInfo earthquakeInfo:earthquakeInfoList)
        {
            if(area.equals(GeoUtil.getCountry(earthquakeInfo.getLongitude(), earthquakeInfo.getLatitude()))||"any".equals(area))
            {
                if(startTime.isBefore(earthquakeInfo.getEarthquakeTime())&&endTime.isAfter(earthquakeInfo.getEarthquakeTime()))
                {
                    if(earthquakeInfo.getMagnitude()>low&&earthquakeInfo.getMagnitude()<high)
                        result.add(earthquakeInfo);
                }
            }
        }
        return result;
    }

    /**
     * Add earthquake string.
     *
     * @param earthquakeInfo the earthquake info
     * @return the string
     */
    @RequestMapping("/addEarthquake")//前端往后传为RequestMapping
    public String addEarthquake(EarthquakeInfo earthquakeInfo)
    {
        Double dividingLine=105.0;
        if(earthquakeInfo.getLongitude()<dividingLine)
        {
            /*西部地区*/
            earthquakeInfo.getIntensity(5.253,1.398,4.164,26,0,2.019,1.398,2.943,8,0);
        }
        else
        {
            /*东部地区*/
            earthquakeInfo.getIntensity(5.019,1.446,4.136,24,0,2.240,1.446,3.070,9,0);
        }
        int result=earthquakeInfoService.insertEarthquakeInfo(earthquakeInfo);
        if(result==0)
        {
            return "fail";
        }
        else
        {
            return "success";
        }
    }
    @GetMapping("/getHistoryList")
    public List<EarthquakeInfo> getHistoryList(String name){
//        log.info("history!");
        System.out.println("历史查询，当前地震名字:"+name);
        return earthquakeInfoService.getHistoryList(name);
    }

    /**
     * Delete earthquake string.
     *
     * @param earthquakeId the earthquake id
     * @return the string
     */
//    @RequestMapping("/deleteById")
    public String deleteEarthquake(Long earthquakeId)
    {
        EarthquakeInfo earthquakeInfo=this.getEarthquakeById(earthquakeId);
        int result=earthquakeInfoService.deleteEarthquakeInfo(earthquakeInfo);
        if(result>0)
        {
            return "success";
        }
        else
        {
            return "fail";
        }
    }
}
